<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/3/31
 * Time: 21:42
 */




class Tree{
    private  $id ;
    private  $name;
    private  $age;
    /*
 *
 * @param  null
 * @return  null
 *
 */
    public function  _construct()
    {
        //构造函数
    }
    /*
 *
 * @param  [array] $arr 参数1
 * @return  null
 *
 */
    public function  init( $arr = array() )
    {
        //初始化函数
        $arrs = array();
        //设置默认参数
        $arrs = [
            'id'=> 1,
            'name'=> 'user',
            'age' => 18
        ];
        if( !(is_array($arr)) )
        {
        //如果传进来的数据不是数组则默认是id
            $this->id = intval( $arr );
        }
        $arrs = array_merge( $arrs , $arr );//合并数组
        //给类的属性赋值
        $this->id = $arrs['id'];
        $this->name = $arrs['name'];
        $this->age = $arrs['age'];
    }
    /*
 *
 * @param  [int] $data 参数1
 * @return  $this
 *
 */
    public function  setID(  $data = 0 )
    {
        //判断是不是数字
        $this->id =is_numeric( $data )? $data : intval($data)  ;
        return $this;
    }
    /*
 *
 * @param  [string] $data 参数1
 * @return  $this
 *
 */
    public function  setName(  $data = 'user' )
    {
        //判断是不是数字
        $this->name =is_string( $data )? $data : strval($data)  ;
        return $this;
    }
    /*
 *
 * @param  [int] data 参数1
 * @return  $this
 *
 */
    public function  setAge(  $data = 18 )
    {
        //判断是不是数字
        $this->age =is_numeric( $data )? $data : intval($data)  ;
        return $this;
    }
};

$tree = new Tree();
$array = array();
$array['id'] = 10;
$array['name'] = 'admin';
$array['age'] = 20;
$tree->init($array);
var_dump($tree);
$tree->setAge()->setID()->setName();
var_dump($tree);






